package com.smartwebx.ha.biz.rainwater.real.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.smartwebx.ha.biz.assign.HaStationAreaManager;
import com.smartwebx.ha.biz.rainwater.real.StationManager;
import com.smartwebx.ha.dal.assign.bean.HaStationArea;
import com.smartwebx.ha.dal.assign.bean.HaStationAreaExample;
import com.smartwebx.ha.dal.assign.bean.HaStationAreaExample.Criteria;
import com.smartwebx.ha.dal.assign.bean.HaStationStationGroup;
import com.smartwebx.ha.dal.assign.bean.HaStationStationGroupExample;
import com.smartwebx.ha.dal.assign.dao.HaStationAreaMapper;
import com.smartwebx.ha.dal.assign.dao.HaStationStationGroupMapper;
import com.smartwebx.ha.dal.realtime.dao.StatGroupMapper;
import com.smartwebx.ha.dal.realtime.dao.StationMapper;
import com.smartwebx.ha.dal.usebean.ShowStationInfo;
import com.smartwebx.ha.dal.usebean.StationInfo;
import com.smartwebx.ha.util.StatEnum;

@Service
public class StationManagerImpl implements StationManager {
	@Autowired
	private StationMapper stationDao;
	@Autowired
	private StatGroupMapper statGroupDao;
	@Autowired
	private HaStationAreaMapper haStationAreaDao;
	@Autowired
	private HaStationAreaManager haStationAreaService;
	@Autowired
	private HaStationStationGroupMapper haStationStationGroupDao;

	@Override
	public StationInfo getStationInfo(String stcd) {
		return stationDao.getStationInfo(stcd);
	}

	private Collection<ShowStationInfo> getStationInfos(
			List<HaStationArea> stationAreas) {
		List<String> stcds = Lists.newArrayList();
		for (HaStationArea stationArea : stationAreas) {
			stcds.add(stationArea.getStationId());
		}

		List<ShowStationInfo> showStationInfos = Lists.newArrayList();

		Collection<ShowStationInfo> stationInfos = stationDao
				.getStationInfosInQuery(stcds);
		for (ShowStationInfo showStationInfo : stationInfos) {
			final String stcd = showStationInfo.getStcd();
			Optional<HaStationArea> findPoint = Iterables.tryFind(stationAreas,
					new Predicate<HaStationArea>() {
						public boolean apply(HaStationArea input) {
							return Objects.equal(input.getStationId(), stcd);
						}
					});
			if (findPoint.isPresent()) {
				showStationInfo.setId(findPoint.get().getId());
			}
			showStationInfos.add(showStationInfo);
		}
		return showStationInfos;
	}

	@Override
	public Collection<ShowStationInfo> getStationInfos(Integer areaId) {
		List<HaStationArea> stationAreas = haStationAreaService
				.getStationAreaByAreaId(areaId);
		return this.getStationInfos(stationAreas);
	}

	public Collection<ShowStationInfo> getStationInfosByStatGroup(
			Integer statGroupId) {
		HaStationStationGroupExample example1 = new HaStationStationGroupExample();
		example1.createCriteria().andStationGroupIdEqualTo(statGroupId);
		List<HaStationStationGroup> statStatGroups = haStationStationGroupDao
				.selectByExample(example1);
		Collection<Integer> statIds = Collections2.transform(statStatGroups,
				new Function<HaStationStationGroup, Integer>() {
					public Integer apply(HaStationStationGroup input) {
						return input.getStatId();
					}
				});
		if (statIds.isEmpty()) {
			return Lists.newArrayList();
		}
		
		HaStationAreaExample haStationAreaExample = new HaStationAreaExample();
		haStationAreaExample.createCriteria().andIdIn(
				Lists.newArrayList(statIds));
		List<HaStationArea> result = haStationAreaDao
				.selectByExample(haStationAreaExample);

		return this.getStationInfos(result);
	}

	@Override
	public Map<Integer, String> getIdStationName(Integer areaId) {
		Map<Integer, String> map = Maps.newHashMap();
		HaStationAreaExample haStationAreaExample = new HaStationAreaExample();
		Criteria criteria = haStationAreaExample.createCriteria();
		criteria.andAreaIdEqualTo(areaId);
		List<HaStationArea> haStationAreas = haStationAreaDao
				.selectByExample(haStationAreaExample);
		List<String> stationIds = Lists.newArrayList();
		for (HaStationArea stationArea : haStationAreas) {
			String stationId = stationArea.getStationId();
			stationIds.add(stationId);
		}

		Collection<ShowStationInfo> stationInfos = stationDao
				.getStationInfosInQuery(stationIds);
		for (int i = 0; i < haStationAreas.size(); i++) {
			HaStationArea haStationArea = haStationAreas.get(i);
			StationInfo stationInfo = Iterables.get(stationInfos, i);
			String name = stationInfo.getStnm() + "(" + stationInfo.getStcd()
					+ ")";
			map.put(haStationArea.getId(), name);
		}

		return map;
	}

	@Override
	public String getGisStations(String stationsKey) {
		String key="";
		switch (stationsKey){
		case "100Stat":
			key="雨量100";
			break;
		case "150Stat":
			key="雨量150";
			break;
		case "188Stat":
			key="雨量188";
			break;
		case "280Stat":
			key="雨量280";
		}
		List<String> stations=stationDao.getGisStations(key);
		StringBuffer stationStr=new StringBuffer();
		String str="";
		for(String station:stations){
			stationStr.append(station);
			stationStr.append(",");
		}
		str=stationStr.toString().substring(0, stationStr.toString().length()-1);
		return str;
	}
	
	@Override
	public String getGis150Stations() {
		List<String> station150=stationDao.getGis150Stations();
		StringBuffer stationStr150=new StringBuffer();
		String str150="";
		for(String station:station150){
			stationStr150.append(station);
			stationStr150.append(",");
		}
		str150=stationStr150.toString().substring(0, stationStr150.toString().length()-1);
		return str150;
	}
	
	@Override
	public String getGis188Stations() {
		List<String> station188=stationDao.getGis188Stations();
		StringBuffer stationStr188=new StringBuffer();
		String str188="";
		for(String station:station188){
			stationStr188.append(station);
			stationStr188.append(",");
		}
		str188=stationStr188.toString().substring(0, stationStr188.toString().length()-1);
		return str188;
	}
}
